# Replication package for 
# "The Economic Leverage of International Organizations in Interstate Disputes"
# Johannes Karreth
# June 30, 2017
# jkarreth@ursinus.edu

# This file: 10_claims_createdata.R
# Purpose: Compile data for analyses of claims

rm(list = ls())
# setwd("...")

library("rio")
library("dplyr")

d <- import("Sources/ICOWclaimdy.dta")

d$yearbeg <- substr(d$begclaim, 0, 4)
d$yearend <- substr(d$endclaim, 0, 4)
d$yearbeg <- as.numeric(d$yearbeg)
d$yearend <- as.numeric(d$yearend)
d$year <- d$yearbeg

d$dyadid <- ifelse(d$chal < d$tgt, d$chal * 1000 + d$tgt, d$tgt * 1000 + d$chal)
d[d$chal == 240 & d$tgt == 255, ]$dyadid <- 265290
d[d$chal == 240 & d$tgt == 255, ]$chal <- 290
d[d$chal == 290 & d$tgt == 255, ]$tgt <- 265


d <- arrange(d, dyadid, year)

# Merge dyadic IGO data

igo <- import("Sources/igo.dyad.diss.dta")
igo <- igo[, c("dyadid", "year", "igo_ingr_use", "igo_bgn_use", "igo_cb_use", "igo_lev3_count_use", "igo_lev2_count_use", "igo_lev3_issue_sum_use", "igo_lev2_issue_sum_use", "igo_ingr1_use", "igo_ingr2_use", "igo_ingr3_use", "igo_bgn1_use", "igo_bgn2_use", "igo_bgn3_use", "igo_cd1_use", "igo_cd2_use", "igo_cd3_use", "igo_env1_use", "igo_env2_use", "igo_is2_use", "igo_is3_use", "igo_mp1_use", "igo_mp2_use", "igo_mp3_use", "igo_msge1_use", "igo_msge2_use", "igo_msge3_use", "igo_sh1_use", "igo_sh2_use", "igo_sh3_use", "igo_er1_use", "igo_er2_use", "igo_er3_use", "igo_gp1_use", "igo_gp2_use", "igo_gp3_use", "igo_gs1_use", "igo_gs2_use", "igo_gs3_use", "igo_lev3_count_use_l1", "igo_lev3_count_use_l2", "igo_lev3_count_use_l5", "igo_ingr_use_l1", "igo_ingr_use_l2", "igo_ingr_use_l5", "igo_bgn_use_l1", "igo_bgn_use_l2", "igo_bgn_use_l5", "igo_mp3_use_l1", "igo_mp3_use_l2", "igo_mp3_use_l5", "igo_lev2_issue_sum_use_l1", "igo_lev2_issue_sum_use_l2", "igo_lev2_issue_sum_use_l5", "igo_lev3_issue_sum_use_l1", "igo_lev3_issue_sum_use_l2", "igo_lev3_issue_sum_use_l5", "igo_lev2_count_use_l1", "igo_lev2_count_use_l2", "igo_lev2_count_use_l5", "igo_gs3_use_l1", "igo_gs3_use_l2", "igo_gs3_use_l5", "igo_gp3_use_l1", "igo_gp3_use_l2", "igo_gp3_use_l5", "igo_bgn3_use_l1", "igo_bgn3_use_l2", "igo_bgn3_use_l5", "igo_ingr3_use_l1", "igo_ingr3_use_l2", "igo_ingr3_use_l5")]
d2 <- merge(x = d, y = igo, by = c("dyadid", "year"), all.x = TRUE, all.y = FALSE)

names(d2)[44:114] <- paste(names(d2)[44:114], "_bc", sep = "")

# Add monadic IGO data

d2$ccode1 <- d2$chal
d2$ccode2 <- d2$tgt

igo1 <- import("Sources/igo.c1.diss.dta")
igo1 <- igo1[, c("ccode1", "year", "c1_igo_cb_use", "c1_igo_lev3_count_use", "c1_igo_lev2_count_use", "c1_igo_lev3_issue_sum_use", "c1igo_cd1_use", "c1igo_cd2_use", "c1igo_cd3_use", "c1igo_env1_use", "c1igo_env2_use", "c1igo_is2_use", "c1igo_is3_use", "c1igo_mp1_use", "c1igo_mp2_use", "c1igo_mp3_use", "c1igo_msge1_use", "c1igo_msge2_use", "c1igo_msge3_use", "c1igo_sh1_use", "c1igo_sh2_use", "c1igo_sh3_use", "c1igo_er1_use", "c1igo_er2_use", "c1igo_er3_use", "c1igo_gp1_use", "c1igo_gp2_use", "c1igo_gp3_use", "c1igo_gs1_use", "c1igo_gs2_use", "c1igo_gs3_use")]

d3 <- merge(x = d2, y = igo1, by = c("ccode1", "year"), all.x = TRUE, all.y = FALSE)

igo2 <- import("Sources/igo.c2.diss.dta")
igo2 <- igo2[, c("ccode2", "year", "c2_igo_cb_use", "c2_igo_lev3_count_use", "c2_igo_lev2_count_use", "c2_igo_lev3_issue_sum_use", "c2igo_cd1_use", "c2igo_cd2_use", "c2igo_cd3_use", "c2igo_env1_use", "c2igo_env2_use", "c2igo_is2_use", "c2igo_is3_use", "c2igo_mp1_use", "c2igo_mp2_use", "c2igo_mp3_use", "c2igo_msge1_use", "c2igo_msge2_use", "c2igo_msge3_use", "c2igo_sh1_use", "c2igo_sh2_use", "c2igo_sh3_use", "c2igo_er1_use", "c2igo_er2_use", "c2igo_er3_use", "c2igo_gp1_use", "c2igo_gp2_use", "c2igo_gp3_use", "c2igo_gs1_use", "c2igo_gs2_use", "c2igo_gs3_use")]

d4 <- merge(x = d3, y = igo2, by = c("ccode2", "year"), all.x = TRUE, all.y = FALSE)

# Add democracy

d4 <- arrange(d4, ccode1, year)

polc1 <- import("Sources/polityiv.c1.dta")

d5 <- merge(x = d4, y = polc1, by = c("ccode1", "year"), all.x = TRUE, all.y = FALSE)

polc2 <- import("Sources/polityiv.c2.dta")

d6 <- merge(x = d5, y = polc2, by = c("ccode2", "year"), all.x = TRUE, all.y = FALSE)

d6 <- arrange(d6, dyadid, year)

d6$jointpol6 <- ifelse(d6$c1polity2 > 6 & d6$c2polity2 > 6, 1, 0)
d6$jointpol7 <- ifelse(d6$c1polity2 > 7 & d6$c2polity2 > 7, 1, 0)

# Add CINC scores

cinc1 <- import("Sources/NMC_v4_0.c1.dta")
cinc1 <- cinc1[, c("ccode1", "year", "cinc1")]

d7 <- merge(x = d6, y = cinc1, by = c("ccode1", "year"), all.x = TRUE, all.y = FALSE)

cinc2 <- import("Sources/NMC_v4_0.c2.dta")
cinc2 <- cinc2[, c("ccode2", "year", "cinc2")]

d8 <- merge(x = d7, y = cinc2, by = c("ccode2", "year"), all.x = TRUE, all.y = FALSE)

# Add ATOP data

atop <- import("Sources/atop3_0ddyr.dta")
atop <- atop[, c("dyadid", "year", "atopally", "defense", "offense", "neutral", "nonagg", "consul", "shareob", "asymm")]

d9 <- merge(x = d8, y = atop, by = c("dyadid", "year"), all.x = TRUE, all.y = FALSE)

d9$atopally <- ifelse(is.na(d9$atopally) == TRUE, 0, d9$atopally)

# Add KGD rivalry data

kgd <- import("Sources/KGD_rivalry_yearly.dta")
kgd <- kgd[, c("dyadid", "year", "rivalrynew", "enduringr")]

d10 <- merge(x = d9, y = kgd, by = c("dyadid", "year"), all.x = TRUE, all.y = FALSE)

d10$rivalrynew <- ifelse(is.na(d10$rivalrynew) == TRUE, 0, d10$rivalrynew)
d10$enduringr <- ifelse(is.na(d10$enduringr) == TRUE, 0, d10$enduringr)

# Add Thompson rivalry data

tho <- import("Sources/thompson.rivalry.yearly.dta")
tho <- tho[, c("dyadid", "year", "rivalry_pos", "rivalry_spat", "rivalry_ide", "rivalry_int", "rivalry_th")]

d11 <- merge(x = d10, y = tho, by = c("dyadid", "year"), all.x = TRUE, all.y = FALSE)

d11$rivalry_th <- ifelse(is.na(d11$rivalry_th) == TRUE, 0, d11$rivalry_th)

# Add trade data

trade <- import("Sources/igotrade.ud1918.dta")
trade <- trade[, c("dyadid", "year", "acra", "acrb", "expab", "eabo", "impab", "iabo", "expba", "ebao", "impba", "ibao", "popa", "rgdpca", "cgdpca", "goa", "popb", "rgdpcb", "cgdpcb", "gob", "tottra", "tottrb")]

d12 <- merge(x = d11, y = trade, by = c("dyadid", "year"), all.x = TRUE, all.y = FALSE)

d12$gdppc_low <- pmin(d12$rgdpca, d12$rgdpcb)
d12$tradedep_c1 <- with(d12, ((impab + expab) * 1000000) / (cgdpca * popa * 1000))
d12$tradedep_c2 <- with(d12, ((impba + expba) * 1000000) / (cgdpcb * popb * 1000))
d12$tradedep_low <- pmin(d12$tradedep_c1, d12$tradedep_c2)
d12$tradedep_low_ln <- log(d12$tradedep_low + 0.000001)

# Add UN affinity

un <- import("Sources/Dyadicdata.tab", format = "tsv")
un <- un[, c("ccode1", "ccode2", "year", "absidealdiff", "s2un", "s3un", "absidealimportantdiff")]
un <- un[un$ccode1 < un$ccode2, ]
un$s2un <- as.numeric(un$s2un)

un$dyadid <- ifelse(un$ccode1 < un$ccode2, un$ccode1 * 1000 + un$ccode2, un$ccode2 * 1000 + un$ccode2)

un <- arrange(un, dyadid, year)
un <- un[, c("dyadid", "year", "absidealdiff", "s2un", "s3un", "absidealimportantdiff")]

un <- mutate(group_by(un, dyadid), 
             absidealdiff_l1 = lag(absidealdiff, 1),
             s2un_l1 = lag(s2un, 1),
             s3un_l1 = lag(s3un, 1))
             
d13 <- merge(x = d12, y = un, by = c("dyadid", "year"), all.x = TRUE, all.y = FALSE)

# Add peace-brokering IGOs

pb <- import("Sources/IGOpb_dyadunit.dta")
pb <- pb[, c("dyadid", "year", "igo_pb_use")]

d14 <- merge(x = d13, y = pb, by = c("dyadid", "year"), all.x = TRUE, all.y = FALSE)

# Add MTOPS

mtops <- import("Sources/mtopsd150.dta")
mtops <- mtops[, c("dyadid", "year", "pacsettg", "pacsettr", "pacsett", "tergen", "terviol", "tertot")]

d15 <- merge(x = d14, y = mtops, by = c("dyadid", "year"), all.x = TRUE, all.y = FALSE)

export(d15, file = "claims_full.csv")